import {
  Select,
  SelectContent,
  SelectItem,
  SelectProps,
  SelectTrigger,
  SelectValue,
} from "./base_selector";

interface DefaultSelectorProps<T = string> extends SelectProps {
  isLoading?: boolean;
  errorMessage?: string | undefined;
  options?: T[];
  errorFlag?: boolean;
  setErrorFlag?: (errorFlag: boolean) => void;
  className?: string;
  placeholder?: string;
}

export const DefaultSelector = ({
  options = [],
  isLoading,
  errorMessage,
  errorFlag = false,
  setErrorFlag,
  className = "",
  onOpenChange,
  placeholder = "",
  ...props
}: DefaultSelectorProps<{ value?: string; label?: string } | string>) => {
  const normalizedOptions = options.map((opt) =>
    typeof opt === "string" ? { value: opt, label: opt } : opt,
  );

  return (
    <Select onOpenChange={onOpenChange} setErrorFlag={setErrorFlag} {...props}>
      <SelectTrigger errorFlag={errorFlag} className={className}>
        <SelectValue placeholder={placeholder}></SelectValue>
      </SelectTrigger>
      <SelectContent
        isLoading={isLoading}
        errorMessage={errorMessage}
        length={options.length}
      >
        {normalizedOptions.map((opt) => (
          <SelectItem key={opt.value} value={opt.value}>
            {opt.label}
          </SelectItem>
        ))}
      </SelectContent>
    </Select>
  );
};

export type { DefaultSelectorProps };
